// pages/talk/talk.js 
// 简单版 
const app = getApp();
Page({ 
  
  data: { 
    

      content: '', 
      // 当前登录者信息 
      login: { 
          id: '2023', 
          user: app.globalData.userInfo.nickname, 
          avatar: app.globalData.userInfo.avatarUrl 
      }, 
      // 聊天信息 
      chatList: [
          { 
              msgId: '2022', 
              nickname: '萌萌', 
              avatar: '../../static/photos/女小人 头像.jpg', 
              message: '你好呀，我是萌萌，我喜欢唱歌与跳舞！', 
              type: 'text' 
          }, 

      ], 
  }, 
  onLoad() { 
      this.scrollToBottom(); 
      this.initChat()
  }, 
  // 输入监听 
  inputClick(e) { 
      this.setData({ 
          content: e.detail.value 
      }) 
  }, 
  createUserMessage(content, msgId) {
    return {
      msgId: msgId, 
      nickname: '萌萌', 
      avatar: '../../static/photos/女小人 头像.jpg', 
      message: content, 
      type: 'text' 
    }
    
    // return {
    //   id: msgUuid(),
    //   type: 'speak',
    //   content,
    //   user,
     
    // }
  },
  update(addList){
    const newList = this.data.chatList.concat(addList)
    this.setData({
      chatList: newList
    })
  },
  sendMessage() {
    var content = this.data.content
    if (!content) {
      return false
    }

    // 设置发送给服务器的信息
    let datas = {
      "messages": [{
        "content": content,
        "role": "user"
      }],
      "session": "bmobchat"
    }
    //执行发送
    this.ChatAi.send(JSON.stringify(datas))
    //在UI中显示对话内容
  //  this.createUserMessage(content, 2023)
  this.update(this.createUserMessage("让我想一想...", 2022))
    //设置输入框内容为空
    // this.setData({
    //   content: ''
    // })
  },
  // 发送监听 
  sendClick() { 
      var that = this; 
      var list = this.data.chatList; 
      // 获取当前时间 
      var date = new Date(); 
      var month = date.getMonth() + 1; 
      var day = date.getDate(); 
      var hour = date.getHours(); 
      var minu = date.getMinutes(); 
      var now1 = month < 10 ? '0' + month : month; 
      var now2 = day < 10 ? '0' + day : day; 
      // 组装数据 
      var msg = { 
          msgId: this.data.login.id, 
          nickname: this.data.login.user, 
          avatar: this.data.login.avatar, 
          message: this.data.content, 
          type: 'text', 
          date: now1 + '-' + now2 + ' ' + hour + ':' + minu 
      } 
      this.setData({ 
          chatList: list.concat(msg) 
      }, () => { 
          that.scrollToBottom(); 
          that.sendMessage()
          that.setData({ 
              content: '' 
          }) 
      }) 
  }, 
  // 滑动到最底部 
  scrollToBottom() { 
      setTimeout(() => { 
          wx.pageScrollTo({ 
              scrollTop: 200000, 
              duration: 3 
          }); 
      }, 100) 
  }, 
  initChat() {
    // 初始化AI链接
    this.ChatAi = wx.Bmob.ChatAI()

    // 返回消息处理，因为是以流的形式返回AI生成的结果，所以这里处理稍微复杂一些
    let msg = ''
    let isStart = true
    this.ChatAi.onMessage((res) => {
      if (isStart) {
        isStart = false
        
      }
      if (res == "done") {
        //  完成
       
        if (msg != "") {
          isStart = true
         
        } 
        msg = '';
      } else {
        msg = msg + res
        // this.update(this.createUserMessage(msg, 2022))
        if(msg!=""){
          this.amendMessage(this.createUserMessage(msg, 2022))
         
        }
       
       
      }
    })

    this.ChatAi.onClose((c) => {
      console.log("连接被关闭，准备重新连接Bmob AI服务");
      this.ChatAi.connect()
    })
  },
  amendMessage(arr){
    var chatList=this.data.chatList
var long=chatList.length
    chatList.pop()
  //  chatList[long-1]=arr
  this.update(arr)



  }
})



